﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using ASRSubsDownloader.Common;
using ASRSubsDownloader.FileHelper;
using ASRSubsDownloader.Properties;
using File = ASRSubsDownloader.FileHelper.File;

namespace ASRSubsDownloader.Forms
{
    public class StartClass : Form
    {

        public StartClass()
        {
            /*var thr = new System.Threading.Thread(showSplash);
            thr.Start();*/
            Config.LoadConfig();
            this.CreateSysTrayIcon();
            this.Hide();
            this.InitializeFolderWatcher();
            //thr.Abort();
        }

        public void showSplash()
        {
            var frm = new Splash();
            frm.ShowDialog();
        }

        private MoviesForm moviesForm;
        private ConfigForm configForm;
        private NotifyIcon trayIcon;
        private ContextMenu trayMenu;
        private FileSystemWatcher m_Watcher;

        protected override void OnLoad(EventArgs e)
        {
            Visible = false; // Hide form window.
            ShowInTaskbar = false; // Remove from taskbar.

            base.OnLoad(e);
        }

        private void OnExit(object sender, EventArgs e)
        {
            Application.Exit();
        }

        protected override void Dispose(bool isDisposing)
        {
            if (isDisposing)
            {
                // Release the icon resource.
                trayIcon.Dispose();
            }

            base.Dispose(isDisposing);
        }

        public void OpenForm(object sender, EventArgs e)
        {
            if (moviesForm == null || moviesForm.IsDisposed)
                moviesForm = new MoviesForm();
            moviesForm.Show();
        }

        public void OpenConfig(object sender, EventArgs e)
        {
            if (configForm == null || configForm.IsDisposed)
                configForm = new ConfigForm();
            
            configForm.Show();
        }

        public void RecacheFiles(object sender, EventArgs e)
        {
            Config.FillFileCache();
        }

        public void CleanBlackList(object sender, EventArgs e)
        {
            Config.FillFileCache();
        }

        public void CreateSysTrayIcon()
        {
            trayMenu = new ContextMenu();
            trayMenu.MenuItems.Add("Sair", OnExit);
            trayMenu.MenuItems.Add("Abrir", OpenForm);
            trayMenu.MenuItems.Add("Configurações", OpenConfig);
            trayMenu.MenuItems.Add("Forçar Recache de arquivos", RecacheFiles);
            trayMenu.MenuItems.Add("Limpar Blacklist", CleanBlackList);
            

            // Create a tray icon. In this example we use a
            // standard system icon for simplicity, but you
            // can of course use your own custom icon too.
            trayIcon = new NotifyIcon();
            trayIcon.Text = "Subtitles Finder";
            trayIcon.Icon = new Icon(Resources.subtitles, 40, 40);
            trayIcon.DoubleClick += OpenForm;
            // Add menu to tray icon and show it.
            trayIcon.ContextMenu = trayMenu;
            trayIcon.Visible = true;
        }

        public void InitializeFolderWatcher()
        {
            FileHelper.FileHelper.trayIcon = trayIcon;
            m_Watcher = new FileSystemWatcher();
            m_Watcher.Path = Config.BaseFolder + "\\";
            m_Watcher.Filter = "*.*";
            m_Watcher.NotifyFilter = NotifyFilters.FileName;
            m_Watcher.IncludeSubdirectories = true;
            m_Watcher.Created += FileHelper.FileHelper.FileCreated;
            m_Watcher.Deleted += FileHelper.FileHelper.FileDeleted;
            m_Watcher.Renamed += FileHelper.FileHelper.FileRenamed;
            m_Watcher.EnableRaisingEvents = true;
        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(StartClass));
            this.SuspendLayout();
            // 
            // StartClass
            // 
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "StartClass";
            this.ResumeLayout(false);

        }

        
    }
}
